package com.lebang.service;

import com.lebang.component.RedisClient;
import com.lebang.domain.MemberInviteRebateRecord;
import com.lebang.entity.LbbDict;
import com.lebang.entity.member.LbbMemberInviteNotice;
import com.lebang.example.LbbMemberInviteNoticeExample;
import com.lebang.mapper.LbbMemberInviteNoticeMapper;
import com.lebang.mapper.MemberInviteRebateRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class MemberInviteRebateRecordService {
    @Autowired
    private MemberInviteRebateRecordMapper memberInviteRebateRecordMapper;
    @Autowired
    private RedisClient redisClient;
    public static final String REBATE_HASH_MEMBER = "REBATE_HASH_MEMBER:";


    //存
    public void settoday(String memberId,int money) {

        boolean on = redisClient.hexists(getKey(), memberId);
        if (!on) {
            redisClient.hset(getKey(), memberId, String.valueOf(money));
            //过期时间修改注释
            //redisClient.expire(getKey(),86400);
        } else {
            redisClient.hincrBy(getKey(), memberId, money);
        }
    }
    public String getKey() {
        LocalDate date = LocalDate.now();
        return REBATE_HASH_MEMBER + date.format(DateTimeFormatter.ISO_DATE);
    }

    //取
    public int gettoday(String memberId) {

        boolean on = redisClient.hexists(getKey(), memberId);
        if (!on) {
            return 0;
        }
        String money = redisClient.hget(getKey(), memberId);
        return Integer.parseInt(money);
    }

    public int getTotalMoney(String tdId, String memberId) {
        return memberInviteRebateRecordMapper.getTotalMoney(tdId,memberId);
    }

    /**
     * 获取徒弟/徒孙今日的累计收益
     * @param memberId
     * @param type
     * @param min
     * @param max
     * @return
     */
    public Integer getTodayMoney(String memberId, int type, LocalDateTime min, LocalDateTime max) {
        return memberInviteRebateRecordMapper.selectByType(memberId,type,min,max);
    }
}
